
#include "Cache/InputConfigureCacheEditor.h"
#include "Misc/FileHelper.h"

DEFINE_LOG_CATEGORY(WGI_ConfigFileEditorLog);


bool InputConfigureLibraryEditor::bAutoSave = false;

FString InputConfigureLibraryEditor::GetNewInputGroupKey()
{
	TArray<FString> Files;
	InputConfigureLibrary::FindInputConfigFiles(Files);
	return FString("NewInputGroup" + FString::FromInt(Files.Num()));
}

FString InputConfigureLibraryEditor::InputConfigFile_Create()
{
	return InputConfigFile_Create(GetNewInputGroupKey());
}

FString InputConfigureLibraryEditor::InputConfigFile_Create(FString NewGroupKey)
{
	FInputConfigureGroupData NewGroupData = FInputConfigureGroupData();
	InputConfigureLibrary::CreateDefaultData(NewGroupData);
	NewGroupData.GroupKey = NewGroupKey;

	TArray<FString> Contents;
	NewGroupData.ConvertToStringArray(Contents);

	FFileHelper::SaveStringArrayToFile(Contents, *FString(InputConfigureLibrary::GetConfigFileDir() + NewGroupKey + ".ini"));

	UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("A new group is created . --- %s"), *NewGroupKey);
	SaveUpdateCode(NewGroupKey);
	return NewGroupKey;
}

bool InputConfigureLibraryEditor::InputConfigFile_Rename(FString OldGroupKey, FString NewGroupKey)
{
	FString fileDic = InputConfigureLibrary::GetConfigFileDir();
	FString Oldfilename = FString(fileDic + OldGroupKey + ".ini");
	FString Newfilename = FString(fileDic + NewGroupKey + ".ini");

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	if (PlatformFile.FileExists(*Newfilename))
	{
		UE_LOG(WGI_ConfigFileEditorLog, Error, TEXT("already have this group ! --- %s"), *NewGroupKey);
		UE_LOG(WGI_ConfigFileEditorLog, Error, TEXT("Rename failure !"));
		return false;
	}

	TArray<FString> Contents;
	if (InputConfigureLibrary::LoadInputConfigFile(*Oldfilename, Contents))
	{
		if (InputConfigureLibrary::GetDefaultConfigGroupName() == OldGroupKey)
		{
			InputConfigureLibrary::SetDefaultConfigGroupName(NewGroupKey);
		}

		PlatformFile.DeleteFile(*Oldfilename);
		FFileHelper::SaveStringArrayToFile(Contents, *Newfilename);
		UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Rename %s -> %s"), *Oldfilename, *NewGroupKey);

		DeleteUpdateCode(OldGroupKey);
		SaveUpdateCode(NewGroupKey);

		return true;
	}

	UE_LOG(WGI_ConfigFileEditorLog, Error, TEXT("This group cannot be read properly ! --- %s"), *OldGroupKey);
	UE_LOG(WGI_ConfigFileEditorLog, Error, TEXT("Rename failure !"));
	return false;
}

void InputConfigureLibraryEditor::InputConfigFile_Save(FInputConfigureGroupData* inGroup)
{
	TArray<FString> Contents;
	inGroup->ConvertToStringArray(Contents);
	FFileHelper::SaveStringArrayToFile(Contents, *FString(InputConfigureLibrary::GetConfigFileDir() + inGroup->GroupKey + ".ini"));
	UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("save successfully . --- %s"), *inGroup->GroupKey);

	SaveUpdateCode(inGroup->GroupKey);
}

void InputConfigureLibraryEditor::InputConfigFile_Save(TSharedPtr<FInputConfigureGroupData> inGroup)
{
	TArray<FString> Contents;
	inGroup->ConvertToStringArray(Contents);
	FFileHelper::SaveStringArrayToFile(Contents, *FString(InputConfigureLibrary::GetConfigFileDir() + inGroup->GroupKey + ".ini"));
	UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("save successfully . --- %s"), *inGroup->GroupKey);

	SaveUpdateCode(inGroup->GroupKey);
}

void InputConfigureLibraryEditor::InputConfigFile_Delete(FString inGroupKey)
{
	if (InputConfigureLibrary::GetDefaultConfigGroupName() == inGroupKey)
	{
		TArray<FString> files;
		InputConfigureLibrary::FindInputConfigFiles(files);
		if (files.IsValidIndex(0))
		{
			FString Name = FPaths::GetBaseFilename(files[0]);
			InputConfigureLibrary::SetDefaultConfigGroupName(Name);
		}
	}

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	PlatformFile.DeleteFile(*FString(InputConfigureLibrary::GetConfigFileDir() + inGroupKey + ".ini"));
	UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("delete successfully . --- %s"), *inGroupKey);


	DeleteUpdateCode(inGroupKey);
}

FString InputConfigureLibraryEditor::InputConfigFile_Copy(FString inGroupKey)
{
	FString fileDic = InputConfigureLibrary::GetConfigFileDir();
	FString Oldfilename = FString(fileDic + inGroupKey + ".ini");

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();

	int fileIndex = 0;
	FString NewName = FString();
	while (true)
	{
		fileIndex++;
		NewName = inGroupKey + "_NewCopy_" + FString::FromInt(fileIndex);
		if (!PlatformFile.FileExists(*FString(fileDic + NewName + ".ini")))
		{
			break;
		}
	}

	PlatformFile.CopyFile(*FString(fileDic + NewName + ".ini"), *Oldfilename);

	UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Copy successfully . --- %s -> %s"), *inGroupKey, *NewName);

	SaveUpdateCode(NewName);
	return NewName;
}

void InputConfigureLibraryEditor::CreateExampleInputGourp()
{
	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	if (PlatformFile.FileExists(*FString(InputConfigureLibrary::GetConfigFileDir() + "ExampleInput 1.ini")))
	{
		UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Create failure.  -> ExampleInput 1"));
	}
	else
	{
		TArray<FString> Contents;
		Contents.Add(FString("FixedInputSettings::"));
		Contents.Add(FString("Enter*false*false*false*false"));
		Contents.Add(FString("BackSpace*false*false*false*false"));
		Contents.Add(FString("OtherInputSettings::"));
		Contents.Add(FString("Left][A*false*false*false*false"));
		Contents.Add(FString("Right][D*false*false*false*false"));
		Contents.Add(FString("Up][W*false*false*false*false"));
		Contents.Add(FString("Down][S*false*false*false*false"));
		Contents.Add(FString("NextPage][E*false*false*false*false"));
		Contents.Add(FString("LastPage][Q*false*false*false*false"));
		Contents.Add(FString("Add][J*false*false*false*false"));
		Contents.Add(FString("Delete][K*false*false*false*false"));
		Contents.Add(FString("Clear][L*false*false*false*false"));
		FFileHelper::SaveStringArrayToFile(Contents, *FString(InputConfigureLibrary::GetConfigFileDir() + "ExampleInput 1.ini"));

		SaveUpdateCode(FString("ExampleInput 1"));
		UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Create successfully.  -> ExampleInput 1"));

		InputConfigureLibrary::SetDefaultConfigGroupName(FString("ExampleInput 1"));
	}

	if (PlatformFile.FileExists(*FString(InputConfigureLibrary::GetConfigFileDir() + "ExampleInput 2.ini")))
	{
		UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Create failure.  -> ExampleInput 2"));
	}
	else
	{
		TArray<FString> Contents;
		Contents.Add(FString("FixedInputSettings::"));
		Contents.Add(FString("Enter*false*false*false*false"));
		Contents.Add(FString("BackSpace*false*false*false*false"));
		Contents.Add(FString("OtherInputSettings::"));
		Contents.Add(FString("Left][A*false*false*false*false"));
		Contents.Add(FString("Right][D*false*false*false*false"));
		Contents.Add(FString("Up][W*false*false*false*false"));
		Contents.Add(FString("Down][S*false*false*false*false"));
		Contents.Add(FString("NextPage][RightBracket*false*false*false*false"));
		Contents.Add(FString("LastPage][LeftBracket*false*false*false*false"));
		Contents.Add(FString("Add][J*false*false*false*false"));
		Contents.Add(FString("Delete][K*false*false*false*false"));
		Contents.Add(FString("Clear][L*false*false*false*false"));
		FFileHelper::SaveStringArrayToFile(Contents, *FString(InputConfigureLibrary::GetConfigFileDir() + "ExampleInput 2.ini"));

		SaveUpdateCode(FString("ExampleInput 2"));
		UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Create successfully.  -> ExampleInput 2"));
	}
}

void InputConfigureLibraryEditor::SaveUpdateCode(FString GroupKey)
{
	FString fileDir = FString(InputConfigureLibrary::GetPluginBaseDir() + "/Saved/UpdateCode/");
	FFileHelper::SaveStringToFile(FGuid::NewGuid().ToString(EGuidFormats::Digits), *FString(fileDir + GroupKey));
	UE_LOG(WGI_ConfigFileEditorLog, Log, TEXT("Update Code"));
}

void InputConfigureLibraryEditor::DeleteUpdateCode(FString GroupKey)
{
	FString fileDir = FString(InputConfigureLibrary::GetPluginBaseDir() + "/Saved/UpdateCode/");
	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	PlatformFile.DeleteFile(*FString(fileDir + GroupKey));
}
